package com.sgb.member.weight;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.member.R;
import com.sgb.member.databinding.LayoutAppendFieldsBinding;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class AppendFieldsView extends FrameLayout {
    private BaseRecycleAdapter<AppendField> adapter;

    public AppendFieldsView(@NonNull Context context) {
        this(context, null);
    }

    public AppendFieldsView(@NonNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AppendFieldsView(@NonNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        intiView();
    }

    private void intiView() {
        LayoutAppendFieldsBinding mBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.layout_append_fields, null, false);
        addView(mBinding.getRoot());
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new BaseRecycleAdapter<AppendField>(getContext(), R.layout.layout_append_fields_item, new ArrayList<>()) {
            @Override
            public void convert(ItemViewHolder holder, AppendField o) {

            }

            @Override
            public void onBindViewHolder(ItemViewHolder holder, int position) {
                super.onBindViewHolder(holder, position);
                ImageView ivDelete = holder.getItem().findViewById(R.id.iv_delete);
                AppCompatEditText editTitle = holder.getItem().findViewById(R.id.edit_title);
                AppCompatEditText editContent = holder.getItem().findViewById(R.id.edit_content);
                editTextChanged(editTitle, getDatas().get(position), position);
                editTextChanged(editContent, getDatas().get(position), position);
                ivDelete.setOnClickListener(v -> {
                    if (datas.size() > 1) {
                        datas.remove(position);
                        notifyItemRemoved(position);
                    } else {
                        datas.clear();
                        notifyDataSetChanged();
                    }
                });
            }

            private void editTextChanged(AppCompatEditText editText, AppendField field, int pos) {
                if (editText.getTag() != null) {
                    editText.removeTextChangedListener((TextWatcher) editText.getTag());
                    editText.clearFocus();
                }
                if (editText.getId() == R.id.edit_title) {
                    editText.setText(field.title);
                } else {
                    editText.setText(field.content);
                }
                TextWatcher watcher = new TextWatcher() {

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (editText.getId() == R.id.edit_title) {
                            getDatas().get(pos).title = s.toString();
                        } else {
                            getDatas().get(pos).content = s.toString();
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                };
                editText.addTextChangedListener(watcher);
                editText.setTag(watcher);
            }
        };
        mBinding.recyclerView.setAdapter(adapter);
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        mBinding.recyclerView.setFocusable(false);
        mBinding.recyclerView.setFocusableInTouchMode(false);
        mBinding.appendLayout.setOnClickListener(v -> {
            if (adapter.getDatas().size() < 5) {
                adapter.addData(new AppendField());
            } else {
                MToast.showToast(getContext(), "最多新增5个字段");
            }
        });
    }

    public List<AppendField> getData() {
        return adapter.getDatas();
    }

    public void setData(List<AppendField> data) {
        adapter.setDatas(data);
        adapter.notifyDataSetChanged();
    }


    public static class AppendField implements Serializable {
        public String title;
        public String content;

        public AppendField() {
        }

        public AppendField(String title, String content) {
            this.title = title;
            this.content = content;
        }
    }
}
